<?php
namespace app\admin\controller;
use app\BaseController;
use think\exception\HttpResponseException;
use think\facade\Db;
use think\facade\Request;
use app\service\commonService;
use auth\Auth;
class Common extends BaseController{
    public function initialize(){
        //获取当前操作控制器名称和方法名称
        $request = Request::instance();
        $con = $request->controller();
        $action = $request->action();
        $name = $con .'/'. $action;
        
        if($name !== 'Index/superedit' && $name !== 'Index/filedel' && $name !== 'Admin/fetchdata'){
            if(!session('id') || !session('name')){
				$url=url('admin/login/login');
                //throw  new HttpResponseException(redirect(url('admin/login/login')));
				//echo "<script>location.href='$url'</script>";exit;
                $this->error('您尚未登录系统',url('admin/login/login'));
            } 
        }

        $auth = (new Auth())->check($name,session('id'));
        //echo $name;
        //echo "<pre>";
        //var_dump($auth);
        //可以直接访问的权限
        $notCheck = array("Roominfo/fetchdata",'Index/index','Index/welcome','Index/superedit','Index/filedel','Admin/fetchdata','Order/getClient','Parkgatebill/getgate');
        //超级管理员可以访问所有权限
        
        if(session('id')!==1){
            if(!in_array($name,$notCheck)){
                if(!$auth){
                    ///echo "无权访问";die;
                    //return "没有权限访问";
                    
                    $this->error('没有权限访问',url('index/welcome'));
                }
            }
        }
        
    }
    
    //根据当前账号信息查询token
    public function pidTokenId(){
        $id = session('id');
        //根据ID查询用户信息
        $adminId = (new commonService())->adminIdInfo($id);
        $usertype = $adminId['usertype'];//角色类型
        $department = $adminId['department'];//所属部门
        if($usertype == 0){//超级管理员

            $com['parking_id'] = (new commonService())->parkingIdList();
        }else if($usertype == 1){//停车yon
            $com['parking_id'] = $department;
        }
        return $com;
    }
    
    public function getParkingNo(){
        $id = session('id');
        //根据ID查询用户信息
        $adminId = (new commonService())->adminIdInfo($id);
        $usertype = $adminId['usertype'];//角色类型
        $department = $adminId['department'];//所属部门
        if($usertype == 0){//超级管理员
           
            $community_id = (new commonService())->communityList();
        }else if($usertype == 1){//服务商
            $com['service_id'] = $department;
            //查询所有商户
            $com['property_id'] = (new commonService())->serviceIdProperty($department);
            //查询所有区域
            $community_id = (new commonService())->propertyIdCommunity($com['property_id']);
        }else if($usertype == 2){//商户
            //查询所有区域
            $community_id = (new commonService())->propertyIdCommunity($department);
        }else if($usertype == 3){//区域
            $community_id[] = $department;
        }
        $community_id = implode(',',$community_id);
        $parkinglist=Db::name('parkinglotinfo')->where('community_id', 'in', $community_id)->order('id desc')->select();
        if(!empty($parkinglist)){
            foreach($parkinglist as $park){
                $parkingNo[] = $park['parkingNo'];
            }
            //将数组转换成字符串
            $parkingNo = implode(',',$parkingNo);
        }else{
            $parkingNo = null;
        }
        return $parkingNo;
    }
}